<template>
  <el-container>
    <el-header>
      <div class="imgDiv">
        <img src="../assets/tree.jpg" alt="" height="50px" width="50px">
        <span style="margin-left: 10px">电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button></el-header>
    <el-container>
      <el-aside :width="collapse?'64px':'200px'">
        <div class="toggle-button" @click="toggle">|||</div>
        <el-menu active-text-color="#3953ff" unique-opened
            :collapse="collapse"
             :collapse-transition="false"
            default-active="2"
            router
            class="el-menu-vertical-demo">
           <el-submenu :index="item.id+''" v-for="(item,index) in menuList" :key="item.id" ><!--index 表示展开菜单的id-->
            <template slot="title">
              <i :class=[iconObj[item.id]]></i>
              <span style="margin-left: 4px">{{item.authName}}</span>
            </template>
             <el-menu-item :index="'/'+item.path+''" v-for="(item,index) in item.children" :key="item.id" >
              <template slot="title">
                <router-link :to="item.path+''" style="text-decoration: none">
                <i class="el-icon-menu"></i>
                <span>{{ item.authName }}</span>
                </router-link>
              </template>
             </el-menu-item>
          </el-submenu>
        </el-menu>

      </el-aside>
      <el-main>
     <!--子路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data(){
    return{
      menuList:[],
      iconObj:{125:'iconfont icon-user',103:'iconfont icon-tijikongjian',
        101:'iconfont icon-shangpin',102:'iconfont icon-danju',145:'iconfont icon-baobiao'},
      collapse:true
    }
  },
  created() {
    this.getMenuList()
  },
  methods:{
    logout(){
      window.sessionStorage.removeItem('token');
      this.$router.push('/login');
    },
    //获取菜单列表
   async getMenuList(){
     const {data:res} = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
     this.menuList = res.data
     console.log(res.data);
   },
    //折叠
    toggle(){
       this.collapse = !this.collapse
    }
  }
}
</script>

<style scoped lang="less">
.el-container{height: 100%}
.el-header{background-color: #55a532;display: flex;justify-content: space-between;align-items: center} /*align-items: center 按钮不会贴边*/
.el-aside{background-color: #a56832;}
.el-main{background-color: #d9d8d8;height: 100%}
.imgDiv{display:flex;align-items: center}/*align-items: center纵向对齐*/
.toggle-button{background-color: #50554e;font-size: 10px;line-height: 20px;text-align: center;letter-spacing: 0.2rem;cursor: pointer}
</style>